﻿#ifndef VIDEOFFMPEG_H
#define VIDEOFFMPEG_H

#include <QObject>
#include <QImage>
#include <QDebug>

#ifdef __cplusplus
extern "C"{
#endif /* __cplusplus */

#include <libavutil/frame.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libavutil/mathematics.h>
#include <libavutil/samplefmt.h>

#ifdef __cplusplus
}
#endif /* __cplusplus */

#define FFMPEG_PIXEL_FORMAT    (AV_PIX_FMT_YUV420P)

class VideoFfmpeg : public QObject
{
    Q_OBJECT
public:
    explicit VideoFfmpeg(int w, int h);
    ~VideoFfmpeg();

    int getNumBytes() { return numBytes; }
    void setRgbBuffer(unsigned char * rgb) { m_rgbBuffer = rgb; }

    int setFrameYUV(unsigned char * pBuff){
        return avpictureFill(m_pFrameYUV, (uint8_t *)pBuff, FFMPEG_PIXEL_FORMAT, width, height);
    }

    bool play(void);

signals:

private:
    int avpictureFill(AVFrame *picture, const uint8_t *ptr, enum AVPixelFormat pix_fmt, int width, int height)
    {
        return av_image_fill_arrays(picture->data, picture->linesize, ptr, pix_fmt, width, height, 1);
    }

private:
    //输入参数
    int width = 0, height = 0;

    AVFrame * m_pFrameRGB;
    AVFrame * m_pFrameYUV;
    struct SwsContext *m_img_convert_ctx;

    //输出参数
    int numBytes = 0;
    unsigned char * m_rgbBuffer = nullptr;
};

#endif // VIDEOFFMPEG_H
